package com.myjob.controller;

import com.myjob.entity.Hr;
import com.myjob.exception.OptionException;
import com.myjob.response.Result;
import com.myjob.service.HrService;
import com.myjob.serviceImpl.HrServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Chen Yuan
 * @create 2021-01-23  14:58
 */
@RestController
@Slf4j
@RequestMapping("/company")
public class HrController {
	@Autowired
	private HrServiceImpl hrService;

	/**
	 * 查询 人事信息
	 * @param currentPage 当前页
	 * @param pageSize 页面大小
	 * @param hrName 人事姓名
	 * @param duty 职责
	 * @return 封装好的结果
	 */
	@GetMapping("/hrInfo")
	public Result selectHr(@RequestParam("currentPage")Integer currentPage,
	                       @RequestParam(value = "pageSize", required = false)Integer pageSize,
	                       @RequestParam(value = "userId", required = false)Long userId,
	                       @RequestParam(value = "companyId", required = false)Long companyId,
	                       @RequestParam(value = "duty", required = false)String duty,
	                       @RequestParam(value = "hrName", required = false)String hrName,
	                       @RequestParam(value = "searchValue", required = false)String searchValue){
		Result result = null;
		try {
			result = hrService.selectHr(currentPage, pageSize, userId, companyId, duty, hrName, searchValue);
		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("操作异常");
		}
		return result;
	}

	/**
	 * 添加 人事信息
	 * @param hr 人事信息
	 */
	@PutMapping("/hrInfo")
	public Result addHr(@RequestBody Hr hr){
		Result result = null;
		try {
			int insert = hrService.addHr(hr);
			if (insert == 3){
				result = Result.succeed("hr信息 添加 成功");
			}else if (insert == -2){
				result = Result.failed("添加失败，Hr 名额已满，请升级会员等级");
			}else if (insert == -3){
				result = Result.failed("添加失败，该用户非普通用户");
			}else if (insert == -4){
				result = Result.failed("输入的手机/邮箱匹配不到用户");
			}else {
				result = Result.failed("hr信息 添加 失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("操作异常");
		}
		return result;
	}

	/**
	 * 更新 人事信息
	 * @param hr 人事信息
	 */
	@PostMapping("/hrInfo")
	public Result updateHr(@RequestBody Hr hr){
		Result result = null;
		try {
			int update = hrService.updateHr(hr);
			if (update == 1){
				result = Result.succeed("hr信息 修改 成功");
			}else {
				result = Result.failed("hr信息 修改 失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("操作异常");
		}
		return result;
	}

	/**
	 * 删除 人事信息
	 * @return 删除结果
	 */
	@DeleteMapping("/hrInfo")
	public Result deleteHr(@RequestParam("UserId") String UserId){
		Result result = null;
		try {
			int update = hrService.deleteHr(UserId);
			if (update == 2){
				result = Result.succeed("hr信息 删除 成功");
			}else {
				result = Result.failed("hr信息 删除 失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new OptionException("操作异常");
		}
		return result;
	}
}
